const multer = require("multer");
const COS = require("cos-nodejs-sdk-v5");
const path = require("path");
const fs = require("fs");
const config = require("../config");
const { SuccessSend } = require("../utils/response");

class Common {
    static async upload(req, res, next) {
        try {
            const storage = multer.diskStorage({
                destination: function (req, file, cb) {
                    if (!fs.existsSync(path.resolve(__dirname, "../uploads"))) {
                        fs.mkdirSync(path.resolve(__dirname, "../uploads"));
                    }
                    cb(null, path.resolve(__dirname, "../uploads"));
                },
                filename: function (req, file, cb) {
                    const uniqueSuffix =
                        Date.now() + "-" + Math.round(Math.random() * 1e9);
                    const ext = path.extname(file.originalname);
                    cb(null, uniqueSuffix + ext);
                },
            });
            // 创建 multer过滤器
            const filter = function (req, file, cb) {
                const ext = path.extname(file.originalname).toLowerCase(),
                    allows = [".png", ".jpg", ".jpeg", ".gif"];
                if (allows.includes(ext)) {
                    cb(null, true);
                } else {
                    cb(new Error("上传格式不支持"));
                }
            };
            const uploader = multer({
                storage: storage,
                limits: { fileSize: 1024 * 1024 * 5, files: 1 },
                fileFilter: filter,
            }).single("file");

            uploader(req, res, function (err) {
                if (err) {
                    return SuccessSend(res, {
                        code: -1,
                        success: false,
                        message: err.message || "上传失败",
                    });
                }
                if (!req.file) {
                    return SuccessSend(res, {
                        code: -1,
                        success: false,
                        message: "参数错误",
                    });
                }
                SuccessSend(res, {
                    code: 200,
                    success: true,
                    message: "上传成功",
                    data: {
                        url: `${config.ASSETS_URL}/${req.file.filename}`,
                    },
                });
            });
        } catch (error) {
            next(error);
        }
    }
    static async ossupload(req, res, next) {
        const cos = new COS({
            SecretId: config.SECRETID,
            SecretKey: config.SECRETKEY,
        });
        cos.uploadFile(
            {
                Bucket: "assets-1257720815",
                Region: "ap-guangzhou",
            },
            function (err, data) {
                if (err) {
                    console.log(err);
                } else {
                    console.log(data);
                    SuccessSend(res, {
                        code: 0,
                        message: "上传成功",
                        data: data,
                    });
                }
            }
        );
    }
}

module.exports = Common;
